  
  
/*  
* Copyright @ 2017 com.iflysse.trains  
* xebest-erp 下午4:41:11  
* All right reserved.  
*  
*/  
  
package com.xebest.web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.github.pagehelper.PageHelper;
import com.xebest.common.base.pagedto.XePageInfoDto;
import com.xebest.common.util.XeBaseUtil;
import com.xebest.common.util.XeRedisUtil;
import com.xebest.constant.XeRedisKey;
import com.xebest.web.mapper.read.XeLogisticsCarReadMapper;
import com.xebest.web.mapper.write.XeLogisticsCarWriteMapper;
import com.xebest.web.model.XeVenCarEntity;
import com.xebest.web.model.XeVenCarLinesEntity;
import com.xebest.web.service.XeLogisticsCarService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**  
 * @desc: xebest-erp  
 * @author: xzd 
 * @createTime: 2017年8月15日 下午4:41:11  
 * @history:  
 * @version: v1.0    
*/  
@Service
public class XeLogisticsCarServiceImpl implements XeLogisticsCarService {
	@Autowired private XeLogisticsCarReadMapper xeLogisticsCarReadMapper;
	@Autowired private XeLogisticsCarWriteMapper xeLogisticsCarWriteMapper;
	private Logger log = LoggerFactory.getLogger(XeLogisticsCarServiceImpl.class);
	@Override
	public List<XeVenCarEntity> queryLogisticsCarList(XeVenCarEntity xeVenCarEntity) {
		List<XeVenCarEntity> carList = xeLogisticsCarReadMapper.queryLogisticsCarList(xeVenCarEntity);
			if (CollectionUtils.isNotEmpty(carList)) {
				for (XeVenCarEntity car : carList) {
					Integer carId = car.getCarId();
					List<XeVenCarLinesEntity> listLine = xeLogisticsCarReadMapper.queryCarLinesByCarId(carId);
					for (XeVenCarLinesEntity carLine : listLine) {
						carLine.setStartProvinceName(XeBaseUtil.baseUtil.getAreaName(carLine.getStartProvinceId()));
						carLine.setStartCityName(XeBaseUtil.baseUtil.getAreaName(carLine.getStartCityId()));
						carLine.setStartAreaName(XeBaseUtil.baseUtil.getAreaName(carLine.getStartAreaId()));
						carLine.setEndProvinceName(XeBaseUtil.baseUtil.getAreaName(carLine.getEndProvinceId()));
						carLine.setEndCityName(XeBaseUtil.baseUtil.getAreaName(carLine.getEndCityId()));
						carLine.setEndAreaName(XeBaseUtil.baseUtil.getAreaName(carLine.getEndAreaId()));
					}
					car.setListLine(listLine);
				}
		}
		return carList;
	}

	@Override
	@Transactional(value = "writeTransactionManager")
	public int insertLogisticsCar(XeVenCarEntity carEntity) {
		//Integer carId = Integer.parseInt(FinanceSeqUtil.seqUtils.getNextSeq(XeVenSeqContents.ERP_VEN_CARID_CODE,"10"));
		int maxCarId = xeLogisticsCarReadMapper.getMaxCarId();
		carEntity.setCarId(maxCarId+1);
		carEntity.setCreateDate(new Date());
		carEntity.setModifyDate(new Date());
		int num = xeLogisticsCarWriteMapper.insertLogisticsCar(carEntity);
		if(num > 0){
			List<XeVenCarLinesEntity> listLine = carEntity.getListLine();
			xeLogisticsCarWriteMapper.insertLogisticsCarLine(listLine,maxCarId+1);
		}
		return num;
	}

	@Override
	@Transactional(value = "writeTransactionManager")
	public int updateLogisticsCar(XeVenCarEntity carEntity) {
		int num = xeLogisticsCarWriteMapper.updateLogisticsCar(carEntity);
		if(num > 0){
			List<XeVenCarLinesEntity> listLine = carEntity.getListLine();
			if(listLine != null && listLine.size() > 0){
			xeLogisticsCarWriteMapper.delLogisticsCarLineByCarId(carEntity.getCarId());
			xeLogisticsCarWriteMapper.insertLogisticsCarLine(listLine,carEntity.getCarId());
			}
		}
		return num;
	}
	
	
	@Override
	public int updateErpLogisticsCar(XeVenCarEntity carEntity) {
		int num = 0;
		//供应商的userCode
		String userCode = carEntity.getUserCode();
		XeVenCarEntity xeVenCarEntity = xeLogisticsCarReadMapper.queryLogisticsCarByCarId(carEntity.getCarId());
		//供应商自己增加的车辆才可以编辑
		if(userCode.equals(xeVenCarEntity.getUserCode())){
			num = xeLogisticsCarWriteMapper.updateLogisticsCar(carEntity);
		}else{
			num = -1;
		}
		return num;
	}
	
	@Override
	public Map<String,Object> queryLogisticsCarByCarId(Integer carId,int pageNum,int pageSize) {
		Map<String,Object> reMap = new HashMap<>();
		XeVenCarEntity xeVenCarEntity = new XeVenCarEntity();
		xeVenCarEntity.setCarId(carId);
		xeVenCarEntity = xeLogisticsCarReadMapper.queryLogisticsCarByCarId(carId);
		// 分页查询路线
		PageHelper.startPage(pageNum, pageSize);
		List<XeVenCarLinesEntity> listLine = xeLogisticsCarReadMapper.queryCarLinesByCarId(carId);
		for (XeVenCarLinesEntity carLine : listLine) {
			carLine.setStartProvinceName(XeBaseUtil.baseUtil.getAreaName(carLine.getStartProvinceId()));
			carLine.setStartCityName(XeBaseUtil.baseUtil.getAreaName(carLine.getStartCityId()));
			carLine.setStartAreaName(XeBaseUtil.baseUtil.getAreaName(carLine.getStartAreaId()));
			carLine.setEndProvinceName(XeBaseUtil.baseUtil.getAreaName(carLine.getEndProvinceId()));
			carLine.setEndCityName(XeBaseUtil.baseUtil.getAreaName(carLine.getEndCityId()));
			carLine.setEndAreaName(XeBaseUtil.baseUtil.getAreaName(carLine.getEndAreaId()));
		}
		xeVenCarEntity.setListLine(listLine);
		XePageInfoDto<XeVenCarLinesEntity> xePageInfo = new XePageInfoDto<XeVenCarLinesEntity>(listLine);
		reMap.put("xePageInfo", xePageInfo);
		reMap.put("xeVenCarEntity", xeVenCarEntity);
		return reMap;
	}
	
	@Override
	@Transactional(value = "writeTransactionManager")
	public int delLogisticsCar(Integer carId) {
		int num = xeLogisticsCarWriteMapper.delLogisticsCar(carId);
		if(num > 0){
			xeLogisticsCarWriteMapper.delLogisticsCarLineByCarId(carId);
			xeLogisticsCarWriteMapper.delcarUser(carId);
		}
		return num;
	}

	@Override
	public List<XeVenCarEntity> venDetail(XeVenCarEntity xeVenCarEntity) {
		List<XeVenCarEntity> list = new ArrayList<>();
		List<String> userCodeList = xeLogisticsCarReadMapper.queryUserCodeRecode(xeVenCarEntity);
		if(userCodeList != null && userCodeList.size() > 0){
			list = xeLogisticsCarReadMapper.venDetail(userCodeList);
		}
		return list;
	}
	@Override
	public List<XeVenCarEntity> logisticsCarInfoList(XeVenCarEntity xeVenCarEntity) {
		
		List<XeVenCarEntity> carList = xeLogisticsCarReadMapper.logisticsCarInfoList(xeVenCarEntity);
		return carList;
		
	}

	@Override
	public XeVenCarEntity logisticsCarDetailByCarId(Integer carId) {
		XeVenCarEntity xeVenCarEntity = new XeVenCarEntity();
		xeVenCarEntity.setCarId(carId);
		xeVenCarEntity = xeLogisticsCarReadMapper.queryLogisticsCarByCarId(carId);
		return xeVenCarEntity;
	}
	
	@Override
	public int checkCarNum(String carPlateNum) {
		int num = xeLogisticsCarReadMapper.checkCarNum(carPlateNum);
		return num;
	}
	
	@Override
	public Map<String,Object> queryErpLogisticsCarList(XeVenCarEntity xeVenCarEntity, int pageNum, int pageSize) {
		Map<String,Object> retMap = new HashMap<String,Object>();
		List<XeVenCarEntity> carList = new ArrayList<>();
		String userCode = xeVenCarEntity.getUserCode();
		List<String> carIds = xeLogisticsCarReadMapper.queryCarIdByUserCode(xeVenCarEntity);
		// 分页查询
		PageHelper.startPage(pageNum, pageSize);
		if(carIds != null && carIds.size() > 0){
			carList = xeLogisticsCarReadMapper.queryErpLogisticsCarList(carIds,xeVenCarEntity);
			//区分是否是供应商本身的车辆 
			for (int i = 0; i < carList.size(); i++) {
				if(userCode.equals(carList.get(i).getUserCode())){
					carList.get(i).setIsVenSelf(1);
				}else{
					carList.get(i).setIsVenSelf(2);
				}
				List<XeVenCarLinesEntity> listLine = xeLogisticsCarReadMapper.queryCarLinesByCarId(carList.get(i).getCarId());
				for (XeVenCarLinesEntity carLine : listLine) {
					carLine.setStartProvinceName(XeBaseUtil.baseUtil.getAreaName(carLine.getStartProvinceId()));
					carLine.setStartCityName(XeBaseUtil.baseUtil.getAreaName(carLine.getStartCityId()));
					carLine.setStartAreaName(XeBaseUtil.baseUtil.getAreaName(carLine.getStartAreaId()));
					carLine.setEndProvinceName(XeBaseUtil.baseUtil.getAreaName(carLine.getEndProvinceId()));
					carLine.setEndCityName(XeBaseUtil.baseUtil.getAreaName(carLine.getEndCityId()));
					carLine.setEndAreaName(XeBaseUtil.baseUtil.getAreaName(carLine.getEndAreaId()));
				}
				carList.get(i).setListLine(listLine);
			}
		}
		XePageInfoDto<XeVenCarEntity> pageInfo = new XePageInfoDto<XeVenCarEntity>(carList);
		retMap.put("pageInfo", pageInfo);
		retMap.put("list", carList);
		return retMap;
	}
	
	/**
	 * 初始化物流车辆   
	 * @author: xzd  
	 * @createTime: 2017年11月6日 下午1:49:10  
	 * @history:  
	 * @param xeVenCarEntity void
	 */
	public void initLogisticsCar(XeVenCarEntity xeVenCarEntity) {
		XeRedisUtil xeRedisUtil = XeRedisUtil.redisUtil;
		try {
			// 先查数据总条数和缓存总条数，不想等更新全部
			int count = xeLogisticsCarWriteMapper.queryLogisticsCarCount();
			Set<String> carSet = XeRedisUtil.redisUtil.hkeys(XeRedisKey.XE_ERP_LOGISTICSCAR_KEY);
			if (count == carSet.size()) {
				String carInfokey = xeVenCarEntity.getCarPlateNum() + "_" + xeVenCarEntity.getMobilePhone();
				String carIdkey = xeVenCarEntity.getCarId() + "";
				SimplePropertyPreFilter cateFilter = new SimplePropertyPreFilter(
						XeVenCarEntity.class, "carPlateNum", "contactPerson", "mobilePhone", "carId");
				String car = JSONObject.toJSONString(xeVenCarEntity,cateFilter);
				log.info( "----一条车辆信息存入缓存----" + car );
				xeRedisUtil.hset(XeRedisKey.XE_ERP_LOGISTICSCAR_KEY, carInfokey, car);
				xeRedisUtil.hset(XeRedisKey.XE_ERP_CARID_KEY, carIdkey, car);
			} else {
				allUpdateCarInfo();
			}
		} catch (Exception e) {
			log.error("初始化物流车辆异常", e);
		}

	}
	
	/**
	 * 更新全部车辆信息放入缓存
	 * @author: xzd  
	 * @createTime: 2017年11月7日 下午2:11:27  
	 * @history:   void
	 */
	public void allUpdateCarInfo(){
		log.info("---------------开始更新全部车辆信息");
		List<XeVenCarEntity> carList = xeLogisticsCarWriteMapper.logisticsCarInfoList(null);
		XeRedisUtil.redisUtil.delete(XeRedisKey.XE_ERP_LOGISTICSCAR_KEY);
		XeRedisUtil.redisUtil.delete(XeRedisKey.XE_ERP_CARID_KEY);
		if(carList != null && carList.size() > 0){
			setCarInfoToRedis( carList );
		}
	}
	
	/**
	 * 车辆信息放入缓存
	 * @author: xzd  
	 * @createTime: 2017年11月7日 下午1:58:00  
	 * @history:  
	 * @param parentId
	 * @param carList void
	 */
	private void setCarInfoToRedis(List<XeVenCarEntity> carList){
		SimplePropertyPreFilter cateFilter = new SimplePropertyPreFilter(
				XeVenCarEntity.class, "carPlateNum", "contactPerson", "mobilePhone", "carId");
		for(XeVenCarEntity car : carList){
			String carInfokey = car.getCarPlateNum() + "_" + car.getMobilePhone();
			String carIdkey = car.getCarId() + "";
			String carStr = JSONObject.toJSONString(car,cateFilter);
			XeRedisUtil.redisUtil.hset(XeRedisKey.XE_ERP_LOGISTICSCAR_KEY, carInfokey, carStr);
			XeRedisUtil.redisUtil.hset(XeRedisKey.XE_ERP_CARID_KEY, carIdkey, carStr);
		}
		
	}
	

}
  
